using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using AdRotatorXNA.Helpers;
using Microsoft.Advertising.Mobile.Xna;
using Microsoft.Xna.Framework.Input;
using AdRotator.Model;
using Microsoft.Advertising;

namespace AdRotatorXNA
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    internal class AdPubcenterComponent : AdComponent
    {
        private static AdPubcenterComponent instance;

        private static DrawableAd _currentAdControl;

        public static bool Initialised = false;

        private AdPubcenterComponent(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }


        public static AdPubcenterComponent Current
        {
            get
            {
                if (AdRotatorXNAComponent.game == null)
                    return null;

                if (instance == null)
                    instance = new AdPubcenterComponent(AdRotatorXNAComponent.game);

                return instance;
            }
        }

        public static void Initialize(Game game, string appID)
        {
            if (Initialised)
                return;

            Initialised = true;

            AdGameComponent.Initialize(game, appID);
            game.Components.Add(AdGameComponent.Current);

            AdGameComponent.Current.Enabled = false;
        }

        public override void UpdateAd(AdInformation adInfo)
        {
            AdInfo = adInfo;

            if (_currentAdControl != null)
            {
                // If we're "updating" to the same ad, there's no work to be done.
                if (_currentAdControl.AdUnitId == adInfo.SecondaryID)
                    return;

                RemoveCurrentControl();
            }

            State = AdState.Initialized;

            _currentAdControl = AdGameComponent.Current.CreateAd(adInfo.SecondaryID, new Rectangle(0, 0, adInfo.Width, adInfo.Height));

            AdGameComponent.Current.Enabled = true;

            _currentAdControl.ErrorOccurred += OnAdError;
            _currentAdControl.AdRefreshed += AdRefreshed;
        }

        protected override void OnEnabledChanged(object sender, EventArgs args)
        {
            if (!Enabled)
                RemoveCurrentControl();

            base.OnEnabledChanged(sender, args);

            AdGameComponent.Current.Enabled = Enabled;
        }

        protected override void UnloadContent()
        {
            RemoveCurrentControl();
            base.UnloadContent();
        }

        public void OnAdError(object sender, AdErrorEventArgs args)
        {
            State = AdState.AdFailed;
        }

        private void RemoveCurrentControl()
        {
            if (_currentAdControl == null)
                return;

            State = AdState.NotInitialized;

            AdGameComponent.Current.RemoveAd(_currentAdControl);

            _currentAdControl.ErrorOccurred -= OnAdError;
            _currentAdControl.AdRefreshed -= AdRefreshed;

            _currentAdControl = null;            
        }

        public void AdRefreshed(object sender, object args)
        {
            State = AdState.AdLoaded;
        }

        public override void UpdatePosition(Vector2 pos)
        {
            System.Diagnostics.Debug.Assert(_currentAdControl != null, "Null Adcontrol");

            _currentAdControl.DisplayRectangle = new Rectangle((int)pos.X, (int)pos.Y, AdInfo.Width, AdInfo.Height);
        }
    }
}
